package com.dongrj.framework.core.model;

import com.dongrj.framework.core.exception.Assert;
import com.dongrj.framework.core.utils.CollectionUtils;
import com.dongrj.framework.core.utils.StringUtils;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;

import java.util.Date;
import java.util.List;

public final class ExampleUtils {
    /**
     * 添加%
     *
     * @param value Like前值
     */
    public static String getLikeValue(String value) {
        if (StringUtils.isEmpty(value)) {
            return null;
        }
        return "%" + value + "%";
    }

    /**
     * 拼接条件
     * (name like '条件' or address like '条件')
     *
     * @param searchKey 搜索关键字
     */
    public static String getLikeCondition(String searchKey, String... cols) {
        if (CollectionUtils.isEmpty(cols)) {
            return null;
        }
        String likeValue = getLikeValue(searchKey);
        if (StringUtils.isEmpty(likeValue)) {
            return null;
        }
        List<String> stringList = Lists.newArrayList();
        for (String col : cols) {
            String item = String.format(col + " like '%s'", likeValue);
            stringList.add(item);
        }
        String join = Joiner.on(" or ").join(stringList);
        return String.format("(%s)", join);
    }


    /**
     * 获取字段As
     *
     * @param value Like前值
     */
    public static String maxAs(String value, String as) {
        Assert.notEmpty(value, "字段不能为空!");
        Assert.notEmpty(as, "别名不能为空!");
        return String.format("max(%s) as %s", value, as);
    }


    /**
     * 获取字段As
     *
     * @param value Like前值
     */
    public static String max(String value) {
        return String.format("max(%s) as %s", value, value);
    }


    /**
     * 获取时间
     *
     * @param time 时间
     */
    public static Date timeToDate(Long time) {
        if (time == null || time < 1) {
            return null;
        }
        return new Date(time);
    }


    /**
     * 获取字段降序字符串
     *
     * @param fieldName 需要降序的字段
     */
    public static String desc(String... fieldName) {
        if (fieldName == null || fieldName.length == 0) {
            Assert.throwBiz("排序字段不能为空!");
        }
        return String.format("%s desc", String.join(",", fieldName));
    }

    /**
     * 判断输入内容是否包含特殊SQL字符
     *
     * @param fieldValue 输入内容
     */
    public static String likeSqlSpecialChar(String fieldValue) {
        String specialChar = "'_\"^*%@#&$.()";
        char[] chars = specialChar.toCharArray();
        for (char aChar : chars) {
            String cs = Character.toString(aChar);
            if (fieldValue.contains(cs)) {
                return cs;
            }
        }
        return null;
    }

    /**
     * 获取字段升序字符串
     *
     * @param fieldName 需要降序的字段
     */
    public static String asc(String... fieldName) {
        if (fieldName == null || fieldName.length == 0) {
            Assert.throwBiz("排序字段不能为空!");
        }
        return String.format("%s asc", String.join(",", fieldName));
    }


    /**
     * 创建时间降序
     */
    public static String descCreateDate() {
        return "create_time desc";
    }

    /**
     * 编辑时间降序
     */
    public static String descUpdateDate() {
        return "update_time desc";
    }

    /**
     * 创建时间降序
     */
    public static <Example extends BaseExample> void descCreateDate(Example example) {
        example.setOrderByClause(descCreateDate());
    }

    /**
     * 创建时间降序
     */
    public static <Example extends BaseExample> void descUpdateDate(Example example) {
        example.setOrderByClause(descUpdateDate());
    }
}
